extends RangeComponent


class_name OverflowRangeComponent
"""
经验条
"""


const key_level: String = "level"
const key_max_level: String = "max_level"
const key_upgrade_value: String = "upgrade_value"


## 当前等级
@export var level: int = 1:
    set(v):
        level = __set_value(key_level, level, v)
## 最大等级
@export var max_level: int = 0:
    set(v):
        max_level = __set_value(key_max_level, max_level, v)
## 每升一级需要的经验
@export var upgrade_value: int = 0:
    set(v):
        upgrade_value = __set_value(key_upgrade_value, upgrade_value, v)


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "level": func(): return level <= max_level if max_level > 0 else true,
    }, super.__check_exports())

func __check_value(property: StringName, v: Variant) -> Variant:
    match property:
        "level": return clampi(v, 0, max_level) if max_level > 0 else v
        "max_level": return max(level, v)
        "upgrade_value": return 0 if upgrade_value < 0 else v
    return super.__check_value(property, v)

# 获得经验
func increase(increment: int) -> void:
    var increment_value: int = increment
    var total: int = 0
    while increment_value > 0:
        var space_value: int = space()
        # 还有容量
        if space_value > increment_value:
            value += increment_value
            total += increment_value
            break
        if max_level > 0 and level >= max_level:
            break
        # 没有容量,升一级
        value = 0
        max_value += upgrade_value
        level += 1
        increment_value -= space_value
        total += space_value
